<?php

namespace App\Modules\Index\Services;


use  App\Modules\Index\Models\Insulin;
use  App\Modules\Index\Models\Medicinal;
use DB;

class InsulinService
{

    /**
     * 添加血糖
     * param array $data
     */
    public static function addInsulin($data=[]){

        return Insulin::addInsulin($data);

    }

    public static function insulinList(){

        $res = Medicinal::where('status',1)->select('id','medicinal_cn','medicinal_en','insulin_type','insulin_preparation','image')->orderBy('id','desc')->get()->toArray();
        return $res;
    }

    public static function userDinner($user_id){
        $res = DB::table('diningtime')->where('user_id',$user_id)->first();
        return $res;
    }

    public static function addMergeInsulin($param){
        $res = DB::table('merge_insulin')->insert($param);
    }

    public static function mergeInsulin(){
        $time  = date('Y-m-d',strtotime('-1day'));
        $start = $time.' 00:00:00';
        $end =  $time.' 23:59:59';

        $data = DB::table('Glucose')->whereBetween('test_at',[$start,$end])->get()->toArray();

        $insulin = Insulin::whereBetween('maked_at',[$start,$end])->get()->toArray();
        if(!empty($data) || empty($insulin)){
            DB::beginTransaction();
            try{
                if($data) {
                    foreach ($data as $value) {
                        $insert['user_id'] = $value->user_id;
                        $insert['meals'] = $value->meals;
                        $insert['time'] = $value->test_at;
                        $insert['glucose'] = $value->glu;
                        if ($value->state == 1) $insert['type'] = '正常';
                        else if ($value->state == 2) $insert['type'] = '偏低';
                        else if ($value->state == 3) $insert['type'] = '偏高';
                        $insert['created_at'] = date('Y-m-d H:i:s');
                        $insert['updated_at'] = date('Y-m-d H:i:s');
                        $res = DB::table('merge_insulin')->insert($insert);
                        if (empty($res)) throw new \Exception('ADD_FAILURE');
                    }
                }

                if($insulin) {
                    foreach ($insulin as $value) {
                        $insert['user_id'] = $value['user_id'];;
                        $insert['meals'] = $value['meals'];;
                        $insert['time'] = $value['maked_at'];
                        $insert['glucose'] = $value['insulin_type'];
                        $insert['type'] = $value['insulin_dosage'].'U';
                        $insert['created_at'] = date('Y-m-d H:i:s');
                        $insert['updated_at'] = date('Y-m-d H:i:s');
                        $res = DB::table('merge_insulin')->insert($insert);
                        if (empty($res)) throw new \Exception('ADD_FAILURE');
                    }
                }
                DB::commit();
            }catch(\Exception $e){

                DB::rollBack();
                return false;
            }

        }

        return true;

    }
}